import java.util.Calendar;
import java.util.Date;

public class LimitOrder extends OrderDecorator {

	public LimitOrder(AbstractOrder abstractOrder) {
		super(abstractOrder);
		this.abstractOrder.setOrderType(OrderType.LIMIT);
	}

	public void setPriceLimit(double priceLimit) {
		abstractOrder.setPriceLimit(priceLimit);
	}

	public void setOrderCategory(OrderCategory orderCategory) {
		abstractOrder.setOrderCategory(orderCategory);
	}

	public void setAccountId(int accountId) {
		abstractOrder.setAccountId(accountId);
	}

	public void setOrderPrice(double orderPrice) {
		abstractOrder.setOrderPrice(orderPrice);
	}

	public boolean setLimit(double priceLimit, int timestamp) {
		abstractOrder.setPriceLimit(priceLimit);

		double orderPrice = abstractOrder.getQuantity()
				* abstractOrder.getPriceLimit();
		abstractOrder.setOrderPrice(orderPrice);
		abstractOrder.setTimestamp(timestamp);

		Calendar calendar = Calendar.getInstance();
		Date startTime = calendar.getTime();
		abstractOrder.setStartTime(startTime);

		calendar.add(Calendar.DATE, timestamp);

		if (calendar.get(Calendar.HOUR_OF_DAY) > Constants.MARKET_CLOSE_HOUR) {
			calendar.set(Calendar.HOUR_OF_DAY, Constants.MARKET_CLOSE_HOUR);
			calendar.set(Calendar.MINUTE, Constants.MARKET_CLOSE_MINUTE);
			calendar.set(Calendar.SECOND, Constants.MARKET_CLOSE_SECOND);
		}

		Date endTime = calendar.getTime();
		abstractOrder.setEndTime(endTime);

		return true;
	}

	public void createOrder(int orderId, OrderCategory orderCategory,
			int accountId, int portfolioId) {
		super.createOrder(orderId, orderCategory, accountId, portfolioId);
	}

	public void display() {
		super.display();
		System.out.println("Order Price: " + abstractOrder.getOrderPrice());
		System.out.println("Start Time: " + abstractOrder.getStartTime());
		System.out.println("End Time: " + abstractOrder.getEndTime());
		System.out.println("Price Limit: " + abstractOrder.getPriceLimit());
	}

	@Override
	public void initializeOrder() {
		super.initializeOrder();
	}

	@Override
	public void activeOrder() {
		super.activeOrder();
	}

	@Override
	public void partialOrder() {
		super.partialOrder();
	}

	@Override
	public void closeOrder(boolean isOrderFulfilled) {
		super.closeOrder(isOrderFulfilled);
	}

	public void updateOrder(StockValueObserver buyer,
			StockValueObserver seller, int matchedQuantity) {
		abstractOrder.updateOrder(buyer, seller, matchedQuantity);
	}
}
